namespace DataClasses
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;

    [CLSCompliant(true)]
    public class Song : ActiveRecord<Song>
    {
        #region Members
        private Guid _idArtist;
        private string _artistName;
        private Guid _idGenre;
        private string _genreName;
        private string _name;
        private int _duration;
        private string _path;
        #endregion

        /// <summary>
        /// Empty constructor of song
        /// </summary>
        public Song() { }

        /// <summary>
        /// Simple constructor of song
        /// </summary>
        /// <param name="name"></param>
        /// <param name="albumName"></param>
        /// <param name="genre"></param>
        /// <param name="artistName"></param>
        /// <param name="duration"></param>
        /// <param name="path"></param>
        public Song(string name, string albumName, string genre, string artistName, int duration, string path)
        {
            Id = Guid.NewGuid();
            Title = name;

            if (!string.IsNullOrEmpty(artistName))
            {
                var tempart = new Artist(artistName);
                IdArtist = tempart.Id;
            }
            Album tempal = null;
            if (!string.IsNullOrEmpty(albumName))
            {
                tempal = new Album(albumName,IdArtist);
            }

            var tempg = new Genre(genre);
            IdGenre = tempg.Id;
            Duration = duration;
            Path = path;
            Save();
            new AlbumSong(tempal.Id, Id);
        }

        #region Properties

        /// <summary>
        /// Keeps id of artist
        /// </summary>
        public Guid IdArtist { get { return _idArtist; } set { _idArtist = value; } }

        /// <summary>
        /// Keeps id of genre
        /// </summary>
        public Guid IdGenre { get { return _idGenre; } set { _idGenre = value; } }
        
        /// <summary>
        /// Keeps title of the song
        /// </summary>
        public string Title { get { return _name; } set { _name = value; } }
        
        /// <summary>
        /// Keeps longness of song
        /// </summary>
        public int Duration { get { return _duration; } private set { _duration = value; } }
        
        /// <summary>
        /// Path of the file
        /// </summary>
        public string Path { get { return _path; } set { _path = value; } }
        
        /// <summary>
        /// Name of artist
        /// </summary>
        public string ArtistName
        {
            get { return _artistName ?? (_artistName = Artist.GetById(IdArtist)[0].Name); }
            set { _artistName = value; }
        }

        /// <summary>
        /// Name of genre
        /// </summary>
        public string GenreName
        {
            get { return _genreName ?? (_genreName = Genre.GetById(IdGenre)[0].Name); }
            set { _genreName = value; }
        }

        public override object[] Objparams
        {
            get
            {
                objects = new object[5];
                objects[0] = Id.ToString();
                objects[1] = IdGenre.ToString();
                objects[2] = Title;
                objects[3] = Duration;
                objects[4] = Path;
                return objects;
            }

            set
            {
                objects = value;
                Id = new Guid(objects[0].ToString());
                IdGenre = new Guid(objects[1].ToString());
                Title = (string)objects[2];
                Duration = (int)objects[3];
                Path = (string)objects[4];
            }
        }

        #endregion

        /// <summary>
        /// Method that converts TimeSpan to Int
        /// </summary>
        /// <param name="span"></param>
        /// <returns></returns>
        public static int ConvertSpan(TimeSpan span)
        {
            var seconds = 0;
            seconds += span.Seconds;
            seconds += span.Minutes * 60;
            seconds += span.Hours * 3600;
            return seconds;
        }

        /// <summary>
        /// Method that converts duration from int to timespan
        /// </summary>
        /// <param name="seconds"></param>
        /// <returns></returns>
        public static TimeSpan ConvertTotalSeconds(int seconds)
        {
            var spansec = 0;
            var spanhours = 0;
            var spanmin = 0;
            while (seconds >= 60)
            {
                if (seconds >= 3600)
                {
                    seconds -= 3600;
                    spanhours += 1;
                    continue;
                }

                seconds -= 60;
                spanmin += 1;
            }

            spansec += seconds;
            return new TimeSpan(spanhours, spanmin, spansec);
        }

        public override string ToString()
        {
            return Title + "\t" + ConvertTotalSeconds(Duration).Duration();
        }
    }
}
